package one.stand.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.vioao.wechat.api.component.wxa.DomainApi;
import com.github.vioao.wechat.bean.entity.component.wxa.Domain;
import com.github.vioao.wechat.bean.response.BaseResponse;
import com.github.vioao.wechat.bean.response.component.wxa.ModifyDomainResponse;
import one.stand.config.ExtJsonConfig;
import one.stand.service.ComponentService;
import one.stand.service.DomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: QIK
 * @CreateDate: 2019/5/13 14:25
 */
@Service
@Transactional
public class DomainServiceImpl extends  PublicBaseService implements DomainService {
    @Lazy
    @Autowired
    ComponentService componentService;
    @Autowired
    ExtJsonConfig extJsonConfig;
    @Override
    public ModifyDomainResponse modifyDomain(String appId) {
        String token=componentService.getAuthorizerToken(appId);
        List<String> reqList=new ArrayList<>();
        reqList.add(extJsonConfig.getBaseUrl());
        reqList.add(extJsonConfig.getLocation());
        List<String> wss=new ArrayList<>();
        wss.add(extJsonConfig.getWssUrl());
        List<String> file=new ArrayList<>();
        file.add(extJsonConfig.getUploadUrl());
        List<String> downloadFile=new ArrayList<>();
        downloadFile.add(extJsonConfig.getDownloadFile());
        DomainApi.modifyDomain(token,JSON.toJSONString(Domain.builder().action("delete").build()));
        Domain domain = Domain.builder().action("add").requestdomain(reqList).uploaddomain(file).downloaddomain(downloadFile).wsrequestdomain(wss).build();
        return DomainApi.modifyDomain(token, JSON.toJSONString(domain));
    }

    @Override
    public ModifyDomainResponse getDomain(String appId) {
        return DomainApi.getDomain(componentService.getAuthorizerToken(appId));
    }
}
